<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet" href="/static/css/login.css">
</head>
<body>
<div class="content">
    <form>
        <input type="hidden" id="uuid" name="uuid">
        <div class="login">
            <div class="header">后台管理系统</div>
            <div class="input">
                <!--<label>账 号：</label>-->
                <input type="text" class="inpt" id="username" name="username" placeholder="请输入账号">
                <br>
                <!--<label>密 码：</label>-->
                <input type="password" class="inpt" id="password" name="password" placeholder="请输入密码">
            </div>
            <div class="verycode">
                <input type="text" name="code" id="code" class="code" placeholder="请输入验证码">
                <img class="code_img" id="codeImg" src="">

            </div>
            <input type="checkbox" name="remember"> 记住密码
            <button class="submit" id="submit">登录</button>
        </div>
    </form>
    <p>Copyright 2024 All Rights Reserved. ICP备1097343896-1</p>
</div>
</body>
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/jquery.cookie.js"></script>
<script type="text/javascript">

    const base_url = '/system/captcha/captcha_image';

    function get_code(base_url) {
        var timestamp = Math.random();
        url = base_url + '/t/' + timestamp;
        $.get({
            url: url,
            headers: {
                'code': 'EQVR4n---'
            },
            success: function (response) {
                if (response.code === 200) {
                    const img = 'data:image/gif;base64,' + response.data.img;
                    $('#codeImg').attr('src', img);
                    $('#uuid').val(response.data.uuid);
                }
            },
            error: function (xhr, status, error) {
                console.error('Error: ', error);
            },
        });
    }

    $('#submit').click(function (event) {
        event.preventDefault(); // 阻止表单默认的提交行为
        do_login();
    });

    function do_login() {
        var formData = new FormData();
        username = $('#username').val();
        password = $('#password').val();
        code = $('#code').val();
        uuid = $('#uuid').val();

        $.ajax({
            type: 'POST',
            url: '/admin/admin/login',
            data: {'uuid':uuid,'username':username,'password':password,'code':code},
            success: function (data) {
                // 登录成功后的处理
                console.log(data.data.token);
                $.cookie("token", data.data.token);
                window.location.href = '/system/system/redirect/type/1';
            },
            error: function (xhr, status, error) {
                // 登录失败后的处理
                console.error(xhr.responseText);
            },
        });
    }


    $('#codeImg').click(function () {
        get_code(base_url)
    })
    get_code(base_url)
</script>
</html>